﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RentAWorker.Business.Interfaces;
using RentAWorker.Business.Impl.Util;
using Dal = RentAWorker.DataAccess.Interfaces;
using RentAWorker.Business.DTO;
using RentAWorker.DataAccess.Interfaces;
using System.Globalization;
using RentAWorker.DataAccess.Entities;
using System.ServiceModel.Activation;

namespace RentAWorker.Business.Impl
{
    [AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
    public class ReferenceDataFacade : IReferenceDataService
    {
        private Dal.ILocationAccess LocationDal;
        private Dal.IMachineTypeAccess MachineTypeDal;
        private Dal.IRentalServiceAccess RentalDal;
        private Dal.IResourceAccess ResourceDal;

        public ReferenceDataFacade()
        {
            LocationDal = DataAccessFactory.GetLocationAccess();
            MachineTypeDal = DataAccessFactory.GetMachineTypeAccess();
            RentalDal = DataAccessFactory.GetRentalServiceAccess();
            ResourceDal = DataAccessFactory.GetResourceAccess();
        }
        
        #region IReferenceDataService Members

        public Location[] GetLocations()
        {
            return LocationDal.GetLocations(string.Empty).ToArray();
        }

        public MachineType[] GetMachineTypes()
        {
            return MachineTypeDal.GetMachineType(string.Empty).ToArray();
        }

        public RentalServiceState[] GetRentStates()
        {
            return RentalDal.GetRentalServiceStates().ToArray();
        }

        public string[] GetMachineStates(string prefix, int maxStates)
        {
            List<string> States = new List<string>();

            foreach (var resource in ResourceDal.GetResources())
            {
                if (resource is Machine)
                {
                    if (((Machine)resource).Status.StartsWith(prefix, true, CultureInfo.CurrentCulture))
                    {
                        if ((States.Count < maxStates)
                                && (!States.Contains(((Machine)resource).Status)))
                        {
                            States.Add(((Machine)resource).Status);
                        }
                    }
                }
            }
            
            return States.ToArray();
        }

        #endregion
    }
}
